package com.cylise.shop;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.cylise.shop.Util.HttpUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.*;

/**
 * Created by Cylise on 2017/7/5.
 */

public class Login extends AppCompatActivity {
    EditText userEditText,passEditText;
    Button loginButton,cancelButton;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        userEditText = (EditText) findViewById(R.id.userEditText);
        passEditText = (EditText) findViewById(R.id.passEditText);
        loginButton = (Button) findViewById(R.id.loginButton);
        cancelButton = (Button) findViewById(R.id.cancelButton);

        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (validate()){
                    try {
                        loginIn();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    private void loginIn() throws IOException {
        loginButton.setEnabled(false);
        cancelButton.setEnabled(false);
        new Thread(new Runnable() {
            @Override
            public void run() {
                String user=userEditText.getText().toString().trim().toUpperCase();
                String pass=passEditText.getText().toString().trim();
                FormBody formBody  = new FormBody.Builder().add("user",user).add("pass",pass).build();
                try {
                    Response response = HttpUtil.postOKHttpRequest(Login.this.getString(R.string.base_url)+"login.jsp",formBody);

                    String result=response.body().string();
                    if (response.isSuccessful()){
                        JSONObject jsonObject = new JSONObject(result);
                        if (jsonObject.getInt("userId")>0){
                            Intent intent = new Intent(Login.this,MainActivity.class);
                            startActivity(intent);
                            finish();
                        }else {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(Login.this,"用户名密码有误",Toast.LENGTH_SHORT).show();
                                    loginButton.setEnabled(true);
                                    cancelButton.setEnabled(true);
                                }
                            });
                        }
                    } else {

                    }
                } catch (IOException e) {
                    Log.d("myshop","网络不正常");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(Login.this,"无法连接到服务器，请检查网络",Toast.LENGTH_SHORT).show();
                            loginButton.setEnabled(true);
                            cancelButton.setEnabled(true);
                        }
                    });
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

    private boolean validate() {
        if (!userEditText.getText().toString().trim().equals("")  &&  !passEditText.getText().toString().trim().equals("")){
            return true;
        } else {

            Toast.makeText(this,"用户名或密码不能为空",Toast.LENGTH_SHORT).show();
            return false;
        }
    }
}
